//
//  DatePicker.m
//  test
//
//  Created by steven yang on 10-11-12.
//  Copyright 2010 kmyhy. All rights reserved.
//

#import "DatePicker.h"


@implementation DatePicker
-(NSString*)text
{
	return textField.text;
}
-(UIDatePicker*)datePicker
{
	return dp;
}
-(UITextField*)textField
{
	return textField;
}
-(NSDate*)date
{
	return dp.date;
}
-(void)setDate:(NSDate *)d
{
	dp.date=d;
}
-(NSDateFormatter*)dateFormatter
{
	return dateFormatter;
}
-(void)setDateFormatter:(NSDateFormatter *)df
{
	dateFormatter=df;
	textField.textAlignment = NSTextAlignmentCenter;
	textField.text=[dateFormatter stringFromDate:dp.date];
}
-(UIDatePickerMode)datePickerMode
{
	return dp.datePickerMode;
}
-(void)setDatePickerMode:(UIDatePickerMode)mode
{
	dp.datePickerMode=mode;
}
- (id)initWithFrame:(CGRect)frame
{

    if ((self = [super initWithFrame:frame]))
	 {
		//默认日期格式为yyyy-MM-dd
		dateFormatter= [[NSDateFormatter alloc] init];
		//[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];//location设置为中国
		[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss +0800"];
		
		//构造一个子视图,用于显示日期选择器
		_subview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
		_subview.backgroundColor=[UIColor clearColor];
		_subview.tag=0;
		//为子视图构造工具栏按钮
        UIBarButtonItem* item = [[[UIBarButtonItem alloc] initWithTitle:@"取消" style:0 target:self action:@selector(btnCloseClick)]autorelease];
//		UIBarButtonItem* item = [[[UIBarButtonItem alloc]   
//								  initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
//								  target:self action:@selector(btnCloseClick)] autorelease];
//	   item.tintColor = [UIColor blueColor];
		NSArray* buttons=[NSArray arrayWithObjects:item,nil];
		//为子视图构造工具栏
		self.subToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 195, 320, 35)];
        
		self.subToolbar.barStyle =UIBarButtonItemStylePlain;//UIBarStyleBlackOpaque;//  UIBarStyleBlackTranslucent  ;
		[_subToolbar sizeToFit];
  		[self.subToolbar setItems:buttons animated:NO]; //把按钮加入工具栏

		[self.subToolbar setHidden:NO];
        [_subview addSubview:self.subToolbar];//把工具栏加入子视图
        
		[self.subToolbar release];
		
		//为子视图构造datePicker
		dp=[[UIDatePicker alloc]init];
		//picker的默认时间为当前时间
		//[self setDate:[NSDate date]];
        //datePicker的坐标显示
		dp.frame=CGRectMake(0, 236, 320,0);
		//picker的默认style为只显示日期
		dp.datePickerMode=UIDatePickerModeDateAndTime;
		//指定datepicker的valueChanged事件
        [dp setHidden:NO];
		[dp addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
		[_subview addSubview:dp];	//把datePicker加入子视图
		
		//上面是子视图，下面是父视图
		
		//文本框
        textField=[[UITextField alloc]initWithFrame:
				   CGRectMake(0, 0, frame.size.width, frame.size.height)];
	   textField.backgroundColor = [UIColor whiteColor];
	   textField.textColor =  [UIColor blackColor];
		textField.delegate=self;
		textField.hidden = NO;
//		textField.enabled=NO;
		textField.borderStyle=UITextAutocorrectionTypeDefault
        ;
		textField.text=[dateFormatter stringFromDate:[self date]];
		textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
		[self addSubview:textField];
	
		
    }
    return self;
}
//当datepicker的值改变时触发
-(void)dateChanged:(id)sender
{
	NSLog(@"date***%@",[(UIDatePicker*)sender date]);
//	date=[sender date];//获取datepicker的日期	
	textField.text=[dateFormatter stringFromDate:[sender date]];
}
//关闭按钮点击时触发
-(void)btnCloseClick
{
	if(_subview!=nil)
	{
		_subview.tag=0;
		[_subview removeFromSuperview];
	}
}

- (void)dealloc {
	[textField release];
	
//	[dateFormatter release];
	[dp release];
	[_subview release];
    [super dealloc];
}
#pragma mark textField delegate method
//当textField被点击时触发
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	if (_subview.tag==0)
	{//若tag标志等于0，说明datepicker未显示
		//置tag标志为1，并显示子视图
		_subview.tag=1;
		[self.superview addSubview:_subview];
	}
	return NO;
}

@end
